import 'package:flutter/material.dart';

/// 应用通用按钮
class AppButton extends StatelessWidget {
  final String text;
  final VoidCallback? onPressed;
  final bool isLoading;
  final bool isEnabled;
  final Color? backgroundColor;
  final Color? foregroundColor;
  final Color? loadingColor;
  final double height;
  final double borderRadius;
  final TextStyle? textStyle;
  final IconData? icon;

  const AppButton({
    Key? key,
    required this.text,
    this.onPressed,
    this.isLoading = false,
    this.isEnabled = true,
    this.backgroundColor,
    this.foregroundColor,
    this.loadingColor,
    this.height = 48.0,
    this.borderRadius = 8.0,
    this.textStyle,
    this.icon,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final effectiveBackgroundColor = backgroundColor ?? Theme.of(context).primaryColor;
    final effectiveForegroundColor = foregroundColor ?? Colors.white;
    final effectiveLoadingColor = loadingColor ?? Colors.white;
    final effectiveTextStyle = textStyle ?? const TextStyle(fontSize: 16, fontWeight: FontWeight.w500);
    
    return SizedBox(
      width: double.infinity,
      height: height,
      child: ElevatedButton.icon(
        onPressed: (isLoading || !isEnabled) ? null : onPressed,
        icon: icon != null && !isLoading ? Icon(icon, size: effectiveTextStyle.fontSize ?? 16 * 1.2) : const SizedBox.shrink(),
        label: isLoading
            ? SizedBox(
                width: 24,
                height: 24,
                child: CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(effectiveLoadingColor),
                  strokeWidth: 2.5,
                ),
              )
            : Text(
                text,
                style: effectiveTextStyle,
              ),
        style: ElevatedButton.styleFrom(
          backgroundColor: effectiveBackgroundColor,
          foregroundColor: effectiveForegroundColor,
          disabledBackgroundColor: effectiveBackgroundColor.withOpacity(0.5),
          disabledForegroundColor: effectiveForegroundColor.withOpacity(0.7),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(borderRadius),
          ),
          padding: const EdgeInsets.symmetric(horizontal: 16),
          elevation: 1,
        ),
      ),
    );
  }
} 